Skip to main content

Join

This page shows how to join two H3 hexagon datasets. This allows merging different datasets together to simplify analysis as each cell contains all the data.

This is particularly helpful when working with:

  • Datasets in different spatial projections
  • Sparse datasets like location data
  • Datasets of different resolutions or types

Joining H3 Datasets

Assumptions

We assume both datasets:

  • Use hex as the H3 hexagon column name (as would be the case for any ingested H3 dataset through Fused)
  • Are accessible at the same hex resolutions

Joining logic

We use DuckDB to join the datasets together.

Using the example from the previous "Aggregate" section, we can join the elevation & crop data layer together.

Elevation dataset (UDF catalog link):

hexdata_avg
59971869368761548714.60947
59971869368787763131.12178
......
59971869368840191923.30812
59971869368866406345.28209

Crop Data Layer dataset (filtering to only show corn, i.e. data_value=1 UDF Catalog Link):

hexdataarea
6001796307636715511507124
60017963076367155113537
.........
6001813562347601911172089
6001913109871329271801

We can join both tables together on the hex column:

@fused.udf
def udf(
bounds: fused.types.Bounds=[-74.556, 40.4, -73.374, 41.029],
res: int = 5,
):
# These are the two datasets coming from the previous "Aggregate" section
elevation = fused.run('copdem_elevation', bounds=bounds, res=res)
cdl = fused.run("reading_cdl_2024_hex_simplified", bounds=bounds, res=res, data_value=1)

common = fused.load("https://github.com/fusedio/udfs/tree/9a3aae2/public/common/")
con = common.duckdb_connect()

qr = f"""
SELECT
e.hex,
e.data_avg as elevation_data,
c.data as cdl_data,
c.total_area as cdl_total_area
FROM elevation as e
LEFT JOIN cdl as c
ON e.hex = c.hex
"""

return con.execute(qr).df()

Returns the following table:

hexelevation_datacdl_datacdl_total_area
599650080954204159289.94321.074533290
599650080954466303257.92331.090340230
............
599671053654220799203.52721.097844060
599671261723484159306.11851.0121949200

Example